package top.itmeans.yes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import top.itmeans.yes.dto.ProductCategoryParam;
import top.itmeans.yes.dto.ProductCategoryWithChildrenItem;
import top.itmeans.yes.entity.Product;
import top.itmeans.yes.entity.ProductCategory;
import top.itmeans.yes.entity.ProductCategoryAttributeRelation;
import top.itmeans.yes.mapper.ProductCategoryAttributeRelationMapper;
import top.itmeans.yes.mapper.ProductCategoryMapper;
import top.itmeans.yes.mapper.ProductMapper;
import top.itmeans.yes.service.ProductCategoryService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 产品分类 服务实现类
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Resource
    private ProductCategoryMapper productCategoryMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductCategoryAttributeRelationMapper productCategoryAttributeRelationMapper;
    @Override
    public int create(ProductCategoryParam productCategoryParam) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setProductCount(0);
        BeanUtils.copyProperties(productCategoryParam, productCategory);
        //没有父分类时为一级分类
        setCategoryLevel(productCategory);
        int count = productCategoryMapper.insert(productCategory);
        //创建筛选属性关联
        List<Long> productAttributeIdList = productCategoryParam.getProductAttributeIdList();
        if(!CollectionUtils.isEmpty(productAttributeIdList)){
            insertRelationList(productCategory.getId(), productAttributeIdList);
        }
        return count;
    }

    /**
     * 批量插入商品分类与筛选属性关系表
     * @param productCategoryId 商品分类id
     * @param productAttributeIdList 相关商品筛选属性id集合
     */
    private void insertRelationList(Long productCategoryId, List<Long> productAttributeIdList) {
        for (Long productAttrId : productAttributeIdList) {
            ProductCategoryAttributeRelation relation = new ProductCategoryAttributeRelation();
            relation.setProductAttributeId(productAttrId);
            relation.setProductCategoryId(productCategoryId);
            productCategoryAttributeRelationMapper.insert(relation);
        }
    }

    @Override
    public int update(Long id, ProductCategoryParam productCategoryParam) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        BeanUtils.copyProperties(productCategoryParam, productCategory);
        setCategoryLevel(productCategory);
        //更新商品分类时要更新商品中的名称
        Product product = new Product();
        product.setProductCategoryName(productCategory.getName());
        LambdaUpdateWrapper<Product> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Product::getProductCategoryId,id);
        productMapper.update(product,updateWrapper);
        LambdaQueryWrapper<ProductCategoryAttributeRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductCategoryAttributeRelation::getProductCategoryId,id);
        productCategoryAttributeRelationMapper.delete(queryWrapper);
        //同时更新筛选属性的信息
        if(!CollectionUtils.isEmpty(productCategoryParam.getProductAttributeIdList())) {
            insertRelationList(id,productCategoryParam.getProductAttributeIdList());
        }
        return productCategoryMapper.updateById(productCategory);
    }

    @Override
    public Page<ProductCategory> getList(Long parentId, Integer pageSize, Integer pageNum) {
        Page<ProductCategory> productCategoryPage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<ProductCategory> productCategoryQueryWrapper = new LambdaQueryWrapper<>();
        productCategoryQueryWrapper.orderByDesc(ProductCategory::getKeywords)
                .eq(ProductCategory::getParentId,parentId);
        return productCategoryMapper.selectPage(productCategoryPage,productCategoryQueryWrapper);
    }

    @Override
    public int delete(Long id) {
        return productCategoryMapper.deleteById(id);
    }

    @Override
    public ProductCategory getItem(Long id) {
        return productCategoryMapper.selectById(id);
    }

    @Override
    public int updateNavStatus(List<Long> ids, Integer navStatus) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setNavStatus(navStatus);
        LambdaUpdateWrapper<ProductCategory> productCategoryUpdateWrapper = new LambdaUpdateWrapper<>();
        productCategoryUpdateWrapper.in(ProductCategory::getId,ids);
        return productCategoryMapper.update(productCategory, productCategoryUpdateWrapper);
    }

    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setShowStatus(showStatus);
        LambdaUpdateWrapper<ProductCategory> productCategoryUpdateWrapper = new LambdaUpdateWrapper<>();
        productCategoryUpdateWrapper.in(ProductCategory::getId,ids);
        return productCategoryMapper.update(productCategory, productCategoryUpdateWrapper);
    }

    @Override
    public List<ProductCategoryWithChildrenItem> listWithChildren() {
        return productCategoryMapper.listWithChildren();
    }

    /**
     * 根据分类的parentId设置分类的level
     */
    private void setCategoryLevel(ProductCategory productCategory) {
        //没有父分类时为一级分类
        if (productCategory.getParentId() == 0) {
            productCategory.setLevel(0);
        } else {
            //有父分类时选择根据父分类level设置
            ProductCategory parentCategory = productCategoryMapper.selectById(productCategory.getParentId());
            if (parentCategory != null) {
                productCategory.setLevel(parentCategory.getLevel() + 1);
            } else {
                productCategory.setLevel(0);
            }
        }
    }
}
